#include "..\CookHeader.h"

int SIZE;
Array <string> stack;
int top = -1;

bool isStackEmpty() {  
	if (top == -1)
		return true;
	else
		return false;
}

bool isStackFull() {   
	if (top >= SIZE - 1)
		return true;
	else
		return false;
}

void push(string data) {   
	if (isStackFull()) {
		return;
	}
	top++;
	stack[top] = data;
}

string pop() {   
	if (isStackEmpty()) {
		return "None";
	}
	string data = stack[top];
	stack[top] = "None";
	top--;
	return data;
}

string peek() {  
	if (isStackEmpty()) {
		return "None";
	}
	return stack[top];
}

bool checkBracket(string expr) {
	for (int i = 0; i < len(expr); i++) {
		string ch = expr.substr(i,1); //   
		
		if (ch == "(" || ch == "[" || ch == "{" || ch == "<")
			push(ch);
		else if (ch == ")" || ch == "]" || ch == "}" || ch == ">") {
			string out = pop();
			if (ch == ")" && out == "(")
				continue;
			else if (ch == "]" && out == "[")
				continue;
			else if (ch == "}" && out == "{")
				continue;
			else if (ch == ">" && out == "<")
				continue;
			else
				return false;
		} else
			continue;
	}

	if (isStackEmpty())
		return true;
	else
		return false;
}

int main() {
	SIZE = 100;
	for (int i = 0; i < SIZE; i++)
		stack.push_back("None");
	
	Array <string> exprAry = { "(A+B)", ")A+B(", "((A+B)-C", "(A+B]", "(<A+{B-C}/[C*D]>)" };
	string expr;
	for (int i = 0; i < len(exprAry); i++) {
		expr = exprAry[i];
		top = -1;
		string res;
		res = checkBracket(expr) ? "true" : "false";
		println(expr + " ==> " + res);
	}
}